$(function(){
    obj.init();
});

var obj = new function() {
    'use strict';
    return {
        dom: {
            submit: null,
            send: null
        },
        countdown: 60,
        init: function () {
            this.dom.submit     = $('#bt_submit');
            this.dom.send       = $('#bt_send_code');

            this.listener();
        },
        listener: function () {
            var $this = this;
            //绑定回车
            document.onkeyup = function(e){
                var ev = document.all ? window.event : e;
                if(ev.keyCode === 13) {
                    $this.dom.submit.click();
                }
            };
            //发送验证码
            $this.dom.send.click(function () {
                $this.funObj().sendCode();
            });
            //找回
            $this.dom.submit.click(function() {
                $this.funObj().submit();
            });
            //渲染背景
            $this.funObj().parseBackground();
        },
        funObj: function(){
            var $this = this;
            return {
                submit: function () {
                    var phone       = $('#phone').val(),
                        code        = $('#code').val(),
                        password    = $('#password').val(),
                        password2   = $('#password2').val();
                    if (!phone) {
                        $this.funObj().showError('手机号不能为空！');
                        return false;
                    }
                    if (phone.length !== 11) {
                        $this.funObj().showError('请输入正确手机号！');
                        return false;
                    }
                    if (!code) {
                        $this.funObj().showError('请输入验证码！');
                        return false;
                    }
                    if (!password) {
                        $this.funObj().showError('密码不能为空！');
                        return false;
                    }
                    if (password.length < 6) {
                        $this.funObj().showError('密码不能少于6位！');
                        return false;
                    }
                    if (password !== password2) {
                        $this.funObj().showError('密码输入不一致！');
                        return false;
                    }
                    xhExtra.ajaxRequest('index/changePasswordByCode', {phone: phone, code: code, password: password}, {check: false}, function (result) {
                        if (result.error) {
                            $this.funObj().showError(result.error);
                            return false;
                        }
                        window.location.href = '/';
                    });
                },
                //发送验证码
                sendCode: function () {
                    var phone = $('#phone').val();
                    if (!phone) {
                        $this.funObj().showError('手机号不能为空！');
                        return false;
                    }
                    if (phone.length !== 11) {
                        $this.funObj().showError('请输入正确手机号！');
                        return false;
                    }
                    xhExtra.ajaxRequest('index/sendCodeForForgetPassword', {phone: phone}, {check: false}, function (result) {
                        if (result.error) {
                            $this.funObj().showError(result.error);
                            return false;
                        }
                    });
                    $this.funObj().setCodeTime();
                },
                //发送验证码倒计时
                setCodeTime: function () {
                    if ($this.countdown === 0) {
                        $this.dom.send.attr('disabled', false);
                        $this.dom.send.text('发送验证码');
                        $this.countdown = 60;
                        return;
                    } else {
                        $this.dom.send.attr('disabled', true);
                        $this.dom.send.text("重新发送(" + $this.countdown + ")");
                        $this.countdown--;
                    }
                    setTimeout(function() {
                        $this.funObj().setCodeTime();
                    }, 1000)
                },
                showError: function (msg) {
                    $.toast({
                        heading: '错误',
                        text: msg,
                        showHideTransition: 'slide',
                        position: 'bottom-center',
                        icon: 'error',
                        stack: false
                    });
                },
                parseBackground: function () {
                    particlesJS('particles', {
                        "particles": {
                            "number": {
                                "value": 80,
                                "density": {
                                    "enable": true,
                                    "value_area": 800
                                }
                            },
                            "color": {
                                "value": "#000"
                            },
                            "shape": {
                                "type": "circle",
                                "stroke": {
                                    "width": 0,
                                    "color": "#000000"
                                },
                                "polygon": {
                                    "nb_sides": 5
                                },
                                "image": {
                                    "width": 100,
                                    "height": 100
                                }
                            },
                            "opacity": {
                                "value": 0.5,
                                "random": false,
                                "aanim": {
                                    "enable": false,
                                    "speed": 10,
                                    "opacity_min": 0.1,
                                    "sync": false
                                }
                            },
                            "size": {
                                "value": 5,
                                "random": true,
                                "anim": {
                                    "enable": false,
                                    "speed": 40,
                                    "size_min": 0.1,
                                    "sync": false
                                }
                            },
                            "line_linked": {
                                "enable": true,
                                "distance": 150,
                                "color": "#000",
                                "opacity": 1,
                                "width": 1
                            },
                            "move": {
                                "enable": true,
                                "speed": 6,
                                "direction": "none",
                                "random": false,
                                "straight": false,
                                "out_mode": "out",
                                "attract": {
                                    "enable": false,
                                    "rotateX": 600,
                                    "rotateY": 1200
                                }
                            }
                        },
                        "interactivity": {
                            "detect_on": "canvas",
                            "events": {
                                "onhover": {
                                    "enable": true,
                                    "mode": "repulse"
                                },
                                "onclick": {
                                    "enable": true,
                                    "mode": "push"
                                },
                                "resize": true
                            },
                            "modes": {
                                "grab": {
                                    "distance": 400,
                                    "line_linked": {
                                        "opacity": 1
                                    }
                                },
                                "bubble": {
                                    "distance": 400,
                                    "size": 40,
                                    "duration": 2,
                                    "opacity": 8,
                                    "speed": 3
                                },
                                "repulse": {
                                    "distance": 200
                                },
                                "push": {
                                    "particles_nb": 4
                                },
                                "remove": {
                                    "particles_nb": 2
                                }
                            }
                        },
                        "retina_detect": true,
                        "config_demo": {
                            "hide_card": false,
                            "background_color": "#b61924",
                            "background_image": "",
                            "background_position": "50% 50%",
                            "background_repeat": "no-repeat",
                            "background_size": "cover"
                        }
                    });
                }
            }
        }
    }
}